import { _decorator, Component, director, Label, Node, tween, v3 } from 'cc';
import { BasePage } from '../../BasePage';
import { Global } from '../../Global';
import { LvLStorage } from '../../LvLStorage';
import { HouseElement } from './HouseElement';
import { HomeBuildCfg } from '../../config/home_build';
import { Clips, events } from '../../Enums';
import { AudioManger } from '../../AudioManger';
const { ccclass, property } = _decorator;

@ccclass('InstructAction')
export class InstructAction extends BasePage {
    @property(Node)
    ele_items: Node[] = [];
    @property(Node)
    house_info: Node = null;
    @property(Label)
    house_name: Label = null;
    @property(Label)
    house_stage: Label = null;

    private total_stage: number = 0;
    start() {
        
    }

    onLoad(): void {
        director.on(events.build_instruct,this.onBuildInstruct,this);
    }

    appearAction(){
        for(let i = 0; i < this.ele_items.length; i++){
            tween(this.ele_items[i])
                .delay(i * 0.1)
                .to(0.2, {scale: v3(1,1,1)})
                .call(() => {
                    if(i== this.ele_items.length - 1){
                        tween(this.house_info)
                            .to(0.2,{position:v3(0,-700,0)})
                            .start();
                    }
                })
                .start();
        }
    }

    open(){
        super.open();
        this.initBuildInfo();
        this.appearAction();
        this.sceneCameraAction(true);
    }

    sceneCameraAction(isAppear: boolean){
        let neg:number = isAppear ? -1 : 1;
        tween(Global.scene_camera)
            .by(0.6,{orthoHeight:200*neg})
            .start();
        tween(Global.scene_camera.node)
            .by(0.6,{position:v3(120*neg,400*neg,0)})
            .start();
    }

    onBuildInstruct(data){
        this.total_stage ++;
        this.house_stage.string = `${this.total_stage} / 25`;
    }

    initBuildInfo(){
        this.total_stage = 0;
        let house_info = LvLStorage.get_house_info();
        for(let i = 0; i < this.ele_items.length; i++){
            let item = this.ele_items[i];
            let stage = house_info.stage[i];
            this.total_stage += stage;
            item.getComponent(HouseElement).initData(1,i+1,stage);
        }

        let house_id = house_info.build_id;
        console.log("house_id",house_id);
        this.house_name.string = HomeBuildCfg[house_id].name;
        this.house_stage.string = `${this.total_stage} / 25`;
    }

    disappearAction(){
        tween(this.house_info)
            .to(0.2,{position:v3(0,-1000,0)})
            .start();
        for(let i = this.ele_items.length-1; i >= 0; i--){
            tween(this.ele_items[i])
                .delay((this.ele_items.length-1-i)*0.1)
                .to(0.2, {scale: v3(0,0,0)})
                .call(() => {
                    if(i== 0){
                        this.close(false);
                        Global.home_view.showInfo();
                        this.sceneCameraAction(false);
                    }
                })
                .start();
        }
    }

    closeCurrPage(): void {
        director.off(events.build_instruct,this.onBuildInstruct,this);
        AudioManger.instance.playSound(Clips.btn_1);
        this.disappearAction();
    }

    update(deltaTime: number) {
        
    }
}

